package cn.iocoder.yudao.module.cms.article.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.cms.article.dal.dataobject.ArticleDO;
import cn.iocoder.yudao.module.cms.article.service.ArticleService;
import cn.iocoder.yudao.module.cms.article.vo.ArticlePageReqVO;
import cn.iocoder.yudao.module.cms.article.vo.ArticleRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 App - 文章")
// 注意避免与 promotion 模块的 AppArticleController 出现同名 Bean 冲突
@RestController("cmsAppArticleController")
@RequestMapping("/cms/article")
@Validated
public class AppArticleController {

    @Resource
    private ArticleService articleService;

    @GetMapping("/list")
    @Operation(summary = "获得文章详情列表")
    @Parameters({
            @Parameter(name = "recommendHot", description = "是否热门", example = "false")
    })
    @PermitAll
    public CommonResult<List<ArticleRespVO>> getArticleList(
            @RequestParam(value = "recommendHot", required = false) Boolean recommendHot) {
        List<ArticleDO> list = articleService.getCategoryListByRecommend(recommendHot);
        return success(BeanUtils.toBean(list, ArticleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得文章详情分页")
    public CommonResult<PageResult<ArticleRespVO>> getArticlePage(ArticlePageReqVO pageReqVO) {
        PageResult<ArticleDO> pageResult = articleService.getArticlePage(pageReqVO);
        List<ArticleRespVO> list = BeanUtils.toBean(pageResult.getList(), ArticleRespVO.class);
        return success(new PageResult<>(list, pageResult.getTotal()));
    }

    @GetMapping("/get")
    @Operation(summary = "获得文章详情")
    @Parameters({
            @Parameter(name = "id", description = "文章编号", example = "1024"),
            @Parameter(name = "title", description = "文章标题", example = "1024"),
    })
    public CommonResult<ArticleRespVO> getArticle(@RequestParam(value = "id", required = false) Long id,
                                                  @RequestParam(value = "title", required = false) String title) {
        ArticleDO article = id != null ? articleService.getArticle(id)
                : articleService.getLastArticleByTitle(title);
        return success(BeanUtils.toBean(article, ArticleRespVO.class));
    }

    @PostMapping("/add-browse-count")
    @Operation(summary = "增加文章浏览量")
    @Parameter(name = "id", description = "文章编号", example = "1024")
    public CommonResult<Boolean> addBrowseCount(@RequestParam("id") Long id) {
        articleService.addArticleBrowseCount(id);
        return success(true);
    }

}
